/**
 * @author wudi
 * @argument 路由配置panel
 * @version 1.0
 * @class RouteManagerGridPanel
 * @extends Ext.Panel
 * @createTime 2012-7-11
 */
Ext.ns('Ext.RouteManager');
Ext.RouteManager.RouteManagerGridPanel = Ext.extend(Ext.Panel, {
	constructor : function(_config) {
		if (_config == null) {
			_config = {};
		};
		Ext.QuickTips.init();
		Ext.apply(this, _config);
		this.sm = new Ext.grid.CheckboxSelectionModel({
					singleSelect : true
				});
		this.cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),this.sm, {		
					header : '开始节点ID',
					dataIndex : 'scrNodeId'
				}, {
					header : '目标节点ID',
					dataIndex : 'destNodeId'
				},{
					header : '节点状态',
					dataIndex : 'status',
					renderer : function(value){
							if(value == 1){
								return "启用";
							} else {
								return "停用";
							}
					}
				}, {
					header : '节点互联编码',
					dataIndex : 'nodeRelationId'
				}, 
				{
					header : '权重',
					dataIndex : 'weight'
				},  {
					header : '备注',
					dataIndex : 'remark'
				}
				/*,{
					header : '修改',
					renderer : function(value){
						return "<a href=# onclick='modfiyInfo()'>修改</a>"
					}
				},{
					header : '删除',
					renderer : function(value) {
						return "<a href=# onclick='return just()'>删除</a>"
					}
				}*/]);
		this.store = new Ext.data.JsonStore({
					url : 'routeManager/queryNodeRouteInfo',
					root : 'root',
					totalProperty :'totalProperty',
					fields : ['scrNodeId','destNodeId','status','remark',
					'nodeRelationId', 'weight']
				});
		this.limit = 15;
		this.pagingBar = new Ext.PagingToolbar({
					store : this.store,
					displayInfo : true,
					pageSize : this.limit,
					displayMsg : '显示从第{0}到第{1}记录,共有{2}条',
					emptyMsg : '没有数据',
					prevText : '上一页',
					nextText : '下一页',
					refreshText : '刷新',
					lastText : '最后页',
					firstText : '第一页',
					beforePageText : '当前页',
					afterPageText : '共{0}页'
				});

		this.form = new Ext.FormPanel({
					frame : true,
					title : '节点路由信息查询',
					height : 150,
					frame : true,
					layout : 'column',
					bodyStyle : 'padding-left:5px;padding-top:10px',
					defaults : {
						defaults : {
							xtype : 'textfield',
							anchor : '90%'
						},
						columnWidth : .33,
						layout : 'form',
						style : 'padding:5px'
					},
					items : [{
								items : [{
									xtype : 'combo',
									emptyText : '请选择',
									fieldLabel : '节点启用状态',
									name : 'status',
									editable : false,
									triggerAction : 'all',
									mode : 'local',
									store : new Ext.data.SimpleStore({
												fields : ['value', 'text'],
												data : [['1', '启用'],
														['0', '停用']]
											}),
									valueField : 'value',
									displayField : 'text'
								}]
							}],
					buttons : [{
								text : '清空',
								iconCls : 'btn-reset',
								handler : this.resetForm,
								scope : this
							},'-',{
								text : '查询',
								iconCls : 'btn-search',
								handler : this.queryGoodsInfo,
								scope : this
							}]
				});
		this.gridPanel = new Ext.grid.GridPanel({
					flex : 1,
					sm:this.sm,
					cm : this.cm,
					title : '信息显示',
					frame : true,
					store : this.store,
					tbar : [{
						text : '修改',
						iconCls : 'btn-edit',
						handler : this.modfiyInfo,
						scope : this
					},'-',{
						text :'新增',
						iconCls : 'btn-add',
						handler : this.addInfo,
						scope : this
					},'-',{
					    text : '删除',
						iconCls : 'btn-delete',
						handler : this.deleteInfo,
						scope : this
					}],
					bbar : this.pagingBar,
					viewConfig : {
						forceFit : true
					},
					loadMask : {
						msg : '正在加载...'
					}
				});
		Ext.RouteManager.RouteManagerGridPanel.superclass.constructor.call(this, {
					layout : 'vbox',
					layoutConfig : {
						align : 'strech',
						pack : 'start'
					},
					width : Ext.getCmp('frmPanel').getInnerWidth(),
					height : Ext.getCmp('frmPanel').getInnerHeight(),
					items : [ this.form,this.gridPanel]

				});
 		this.store.load({
					params : {
						start : 0,
						limit : this.limit
					}
				});
	},
	
	// 查询按钮事件
	queryGoodsInfo : function() {
		this.store.baseParams.status = this.form.find('name',
				'status')[0].getValue();
		this.store.load({
					params : {
						start : 0,
						limit : this.limit
					}
				});

	},
	// 清空
	resetForm : function() {
		this.form.getForm().reset();
	},
	
	//删除某条记录
	deleteInfo:function(){
			var rec = this.gridPanel.getSelectionModel().getSelected();
			var store =this.store;
				if (rec != undefined) {
					Ext.Msg.confirm('提示', '确定要删除选中的数据', function(btn) {
								if (btn == 'yes') {
									var scrNodeId = rec.get('scrNodeId');
									var destNodeId = rec.get('destNodeId');
									Ext.Ajax.request({
												url : 'routeManager/deleteNodeRelInfo',
												params : {
													scrNodeId : scrNodeId,
													destNodeId : destNodeId
												},
												method : 'post',
												success : function(response, optionse) {
													Ext.MessageBox.alert('提示', '删除成功！');
													
													store.reload();
												},
												failure : function(response, optionse) {
													Ext.MessageBox.alert('提示', '删除失败！');
												}, 
												scope : this
											})
								}
							}, this);
				} else {
					Ext.MessageBox.show({
								title : '警告',
								msg : '请选择一条记录',
								icon : Ext.MessageBox.WARNING,
								buttons : Ext.MessageBox.OK
							})
				}
	
	},
	
	//打开修改信息
	modfiyInfo:function(){
	 var rec = this.gridPanel.getSelectionModel().getSelected();
	 
	 if (rec != undefined) {
		 
			var scrNodeId = rec.get('scrNodeId');
			var destNodeId = rec.get('destNodeId');
			var win = new Ext.RouteManager.RouteManagerWin({
			goodsStore:this.store,
			scrNodeId:rec.get('scrNodeId'),
			destNodeId:rec.get('destNodeId')
			});
			win.setFlag('edit');
			win.loadData(scrNodeId,destNodeId);
			win.show();
		} else {
			Ext.MessageBox.show({
						title : '警告',
						msg : '请选择一条记录',
						icon : Ext.MessageBox.WARNING,
						buttons : Ext.MessageBox.OK
					})
		}
	},
	//打开新增信息
	addInfo:function(){
	 
			var win = new Ext.RouteManager.RouteManagerWin({
				goodsStore:this.store,
				});
			win.setFlag('add');
			win.show();
	}
})


